<?php
/**
 * 角色操作基类
 * User: Administrator
 * Date: 2018\8\31 0031
 * Time: 11:23
 */

namespace app\Lib\Model\Role\RoleManage;


use app\Lib\InterfaceIn\Role\RoleHandle;
use app\Lib\InterfaceIn\Role\RoleManage\RoleOperatingBaseHandle;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\DbModel\Role;
use app\validate\RoleValidate;

abstract class RoleOperatingBase implements RoleOperatingBaseHandle
{
    /** @var RoleHandle $parentObj */
    protected $parentObj;
    /** @var UserHandle $user */
    protected $user;
    protected $status = 1;
    protected $listOrder = 0;
    protected $name;
    protected $remark;
    protected $roleValidate;
    protected $roleDb;

    /**
     * 获取上级的角色类
     * @param RoleHandle $parentObj
     * @return $this
     */
    public function setParentObj(RoleHandle $parentObj)
    {
        $this->parentObj = $parentObj;
        return $this;
    }


    /**
     * 设置状态
     * @param $status
     * @return $this
     */
    public function setStatus($status)
    {
        $this->status = $status;
        return $this;
    }

    /**
     * 设置排序
     * @param $listOrder
     * @return $this
     */
    public function setListOrder($listOrder)
    {
        $this->listOrder = $listOrder;
        return $this;
    }

    /**
     * 设置角色名称
     * @param $name
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * 设置备注信息
     * @param $remark
     * @return $this
     */
    public function setRemark($remark)
    {
        $this->remark = $remark;
        return $this;
    }

    /**
     * 设置操作人
     * @param UserHandle $user
     * @return $this
     */
    public function setOpUser(UserHandle $user)
    {
        $this->user = $user;
        return $this;
    }

    /**
     * 获取数据库
     * @return Role
     */
    protected function getRoleDb(){
        if(empty($this->roleDb)){
            $this->roleDb = new Role();
        }
        return $this->roleDb;
    }

    /**
     * 获取验证表单
     * @return RoleValidate
     */
    protected function getRoleValidate(){
        if(empty($this->roleValidate)){
            $this->roleValidate = new RoleValidate();
        }
        return $this->roleValidate;
    }

    /**
     * 整理数组
     * @return array
     */
    protected function getHandleArray(){
        $model = $this->getRoleDb();
        $data = [
            $model->_name => $this->name,
            $model->_status => 1,
            $model->_list_order =>$this->listOrder,
            $model->_level=>$this->getLevel(),
            $model->_parent_id => empty($this->parentObj)?0:$this->parentObj->getRoleId(),
            $model->_remark=>$this->remark,
        ];
        return $data;
    }

    /**
     * 获取角色等级
     * @return  int
     */
    protected function getLevel(){
        return  empty($this->parentObj)?1:($this->parentObj->getLevel())+1;
    }

}